package com.qqtech.qquan.coupon.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.coupon.model.CouponUser;
import com.qqtech.qquan.coupon.model.CouponUserQuery;
import com.qqtech.qquan.coupon.model.CouponUserVo;

@Repository
public class CouponUserDaoImpl extends BaseDaoImpl<CouponUser> implements CouponUserDao {
	public static final Logger log = LoggerFactory.getLogger(CouponUserDaoImpl.class);

	@Override
	public CouponUserVo getBaseByCouponNo(String couponNo) {
		return sqlSessionTemplate.selectOne(getSqlName("selectByCouponNo"), couponNo);
	}

	@Override
	public int updateByCouponNo(CouponUser couponUser) {
		return sqlSessionTemplate.update(getSqlName("updateByCouponNo"), couponUser);
	}
    @Override
    public Integer queryUnusedCount(CouponUser couponUser){
    	return sqlSessionTemplate.selectOne(getSqlName("selectCount"), BeanUtil.toMap(couponUser));
    }
	@Override
	public int queryCountAdmin(CouponUser query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountAdmin"), BeanUtil.toMap(query));
	}
	@Override
	public int queryCount4UserNoRepeat(CouponUser query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCount4UserNoRepeat"), BeanUtil.toMap(query));
	}
	
	@Override
	public Page<CouponUserVo> queryPageListAdmin(CouponUser query,
			Pageable pageable) {
		List<CouponUserVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
		return new PageImpl<CouponUserVo>(contentList, pageable, this.queryCountAdmin(query));
	}

	@Override
	public List<CouponUserVo> getLeftBase(CouponUserQuery couponUserQuery) {
		return sqlSessionTemplate.selectList(getSqlName("selectLeftBase"), couponUserQuery);
	}

	@Override
	public void autoExpire() {
		sqlSessionTemplate.update(getSqlName("updateByExpire"));
	}

	@Override
	public List<CouponUserVo> queryListAdmin(CouponUser query, Pageable pageable) {
		return sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
	}

	@Override
	public CouponUserVo getMaxFullPriceMinusPrice(CouponUserQuery couponUserQuery) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLeftBaseMaxMinusPrice"), couponUserQuery);
	}

	@Override
	public Integer queryCountByUserId(CouponUser query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCount"), BeanUtil.toMap(query));
	}

	@Override
	public List<CouponUserVo> queryCountByUserIds(CouponUserQuery couponUserQuery) {
		return sqlSessionTemplate.selectList(getSqlName("selectCountByUserIds"), BeanUtil.toMap(couponUserQuery));
	}
}
